<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
//订单控制器
class ReferController extends CommonController{
	private $address;
	private $orders;
	//构造函数
	public function __init(){
		$this->address = new \Common\Model\Address;
		$this->orders = new \Common\Model\Uorders;
		//调用CommonController里面的构造函数
		parent::__init();
	}
	//订单页面展示
	public function index(){
		if($_SESSION['uid']){
			//如果是老用户，就在地址表里查出其默认的收货地址
			$address = $this->address->where("user_uid={$_SESSION['uid']}")->orderBy('statu','DESC')->limit(3)->get();			
			foreach ($address as $k => $v) {
				//把地址之间的逗号去掉
				$address[$k]['addresses'] = str_replace(',', '', $v['addresses']);
				//加密电话号码	
				$address[$k]['telephone'] = substr_replace($v['telephone'], '****', 4,4);
				//不加密电话号码，放到隐藏域
				$address[$k]['tel'] = $v['telephone'];
			}
			//查询默认收货地址
			$defaddress = $this->address->where("user_uid={$_SESSION['uid']} and statu=1")->find();
			View::with('defaddress',$defaddress);
			View::with('address',$address);
		}
		
		//如果用户提交新的地址
		if(IS_POST){
		    //p(Q("post."));
			//获得商品gid，查询			
			$gid = Q('post.gid');
			//查询列表图
			$listview = Db::table('goods')->where("gid={$gid}")->pluck('listview');
			//框架里定义的新数组把隐藏域里传过来的数组压入新的数组
		    $data = array(
				'id'    	=> Q('post.gid',0,'intval'),
				'name'  	=> Q('post.gname',''),
				'num'	 	=> Q('post.num',0,'intval'),				
				'price' 	=> Q('post.shopprice',0,'intval'),
				'options'   =>array(
								'gnum'    => Q('post.gnum',''),
								'color'	  => Q('post.color',''),
								'size'    => Q('post.size',''),
								'listview'=> $listview,
							 ),
				
			);
			Cart::add($data);
			
		}
			//删除session里的cart数组中的某个元素
			//unset($_SESSION['cart']['goods']['d5a5b80a']);
			//用框架里的购物车类把新数组存入session里面
			
			$goods = isset($_SESSION['cart']['goods']) ? $_SESSION['cart']['goods']:array();
			View::with('goods',$goods);
			//分配商品总价
			$totals = isset($_SESSION['cart']['total']) ? $_SESSION['cart']['total']:0;
			View::with('totals',$totals);
	    View::make();
	}
    //修改地址
    public function edit(){
        if(IS_POST){
        	
        }
    }
	//删除地址
	public function del(){
	  //接收did
	    $did = Q('get.did',0,'intval');
		$this->address->where("did={$did}")->delete();
		View::success('删除成功');
	}
	
	
}

?>	
	
	
	
	
	
	
	
	
	
	